﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel;

namespace DoAnWeb.DTO
{
    public class MyCart
    {
        readonly BindingList<CartItem> m_Items;

        public BindingList<CartItem> Items
        {
            get { return m_Items; }
        }

        public MyCart()
        {
            m_Items = new BindingList<CartItem>();
        }

        public void AddItem(CartItem _Item)
        {
            CartItem ExistsItem = m_Items.SingleOrDefault(c => c.MaTinKhuyenMai == _Item.MaTinKhuyenMai);
            if (ExistsItem != null)
                ExistsItem.SoLuong += _Item.SoLuong;
            else
                m_Items.Add(_Item);
        }

        public void UpdateItem(int _MaTinKhuyenMai, int _SoLuong)
        {
            CartItem ItemToUpdate = m_Items.SingleOrDefault(c => c.MaTinKhuyenMai == _MaTinKhuyenMai);
            ItemToUpdate.SoLuong = _SoLuong;
        }

        public void DeleteItem(int _MaTinKhuyenMai)
        {
            CartItem ItemToDelete = m_Items.Single(c => c.MaTinKhuyenMai == _MaTinKhuyenMai);
            m_Items.Remove(ItemToDelete);
        }

        public int GetNumberOfItems()
        {
            return m_Items.Sum(c => c.SoLuong);
        }
    }
}